package com.chery.media.network.http

import com.chery.media.app.base.BaseResult
import com.chery.media.library.base.BaseModel
import com.chery.media.library.extend.toJson
import com.chery.media.network.model.BannerBean
import com.chery.media.network.model.MusicDataBean
import com.chery.media.network.model.VersionBean
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

class AppRepository private constructor(
    private val netWork: AppNetWork
) : BaseModel() {

    /**
     * 获取版本信息
     */
    fun getVersionData(): Flow<BaseResult<VersionBean>> {
        val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
        return netWork.getVersionData(RequestBody.create(mediaType, "{}"))
    }

    /**
     * 获取首页banner
     */
    fun getBannerData(map: Map<String, Any>): Flow<BaseResult<BannerBean>> {
        val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
        return netWork.getBannerData(RequestBody.create(mediaType, map.toJson()))
    }

    /**
     * 获取音乐列表
     */
    fun getMusicData(map: Map<String, Any>): Flow<BaseResult<MusicDataBean>> {
        val mediaType = "application/json; charset=utf-8".toMediaTypeOrNull()
        return netWork.getMusicData(RequestBody.create(mediaType, map.toJson()))
    }

    companion object {

        @Volatile
        private var INSTANCE: AppRepository? = null

        fun getInstance(netWork: AppNetWork) =
            INSTANCE ?: synchronized(this) {
                INSTANCE ?: AppRepository(netWork).also { INSTANCE = it }
            }
    }

}